/*     */ package com.zimbra.cs.iochannel;
/*     */ 
/*     */ import com.zimbra.common.iochannel.Client;
/*     */ import com.zimbra.common.iochannel.Client.PeerServer;
/*     */ import com.zimbra.common.iochannel.IOChannelException;
/*     */ import com.zimbra.common.iochannel.Server.NotifyCallback;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.LogFactory;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import java.io.IOException;
/*     */ import java.nio.ByteBuffer;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class MessageChannel
/*     */ {
/*     */   private com.zimbra.common.iochannel.Server server;
/*     */   private Client client;
/*     */   private boolean running;
/*     */   
/*     */   public static MessageChannel getInstance()
/*     */   {
/*  46 */     synchronized (MessageChannel.class) {
/*  47 */       if (instance == null) {
/*  48 */         instance = new MessageChannel();
/*     */       }
/*     */     }
/*  51 */     return instance;
/*     */   }
/*     */   
/*     */   public synchronized void startup() throws ServiceException, IOException {
/*  55 */     if (!this.running) {
/*  56 */       ZcsConfig config = new ZcsConfig();
/*  57 */       this.server = com.zimbra.common.iochannel.Server.start(config);
/*  58 */       this.client = Client.start(config);
/*  59 */       this.server.registerCallback(new MessageChannelCallback(null));
/*  60 */       this.running = true;
/*     */     }
/*     */   }
/*     */   
/*     */   public synchronized void shutdown() {
/*  65 */     this.server.shutdown();
/*  66 */     this.client.shutdown();
/*  67 */     this.running = false;
/*     */   }
/*     */   
/*     */   public boolean isRunning() {
/*  71 */     return this.running;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public void sendMessage(Message message)
/*     */   {
/*  79 */     String accountId = message.getRecipientAccountId();
/*  80 */     Provisioning prov = Provisioning.getInstance();
/*     */     try {
/*  82 */       Account targetAccount = prov.getAccountById(accountId);
/*  83 */       if (targetAccount == null) {
/*  84 */         log.error("account %s doesn't exist", new Object[] { accountId });
/*  85 */         return;
/*     */       }
/*  87 */       com.zimbra.cs.account.Server targetServer = targetAccount.getServer();
/*  88 */       sendMessage(targetServer, message);
/*     */     } catch (ServiceException e) {
/*  90 */       log.error("can't find server for account %s", accountId, e);
/*     */     }
/*     */   }
/*     */   
/*     */   public void sendMessage(com.zimbra.cs.account.Server server, Message message) {
/*     */     try {
/*     */       String peerHostname;
/*     */       Client.PeerServer peer;
/*  98 */       if ((server == null) || (this.client == null) || ((peerHostname = server.getServiceHostname()) == null) || ((peer = this.client.getPeer(peerHostname)) == null))
/*     */       {
/*     */ 
/* 101 */         log.error("no client available for server %s", new Object[] { server.getServiceHostname() }); return; }
/*     */       Client.PeerServer peer;
/*     */       String peerHostname;
/* 104 */       peer.sendMessage(message.serialize());
/*     */     } catch (IOChannelException e) {
/* 106 */       log.warn("MessageChannel: " + e.getMessage());
/*     */     } catch (IOException e) {
/* 108 */       log.error("can't send notification", e);
/*     */     }
/*     */   }
/*     */   
/*     */   private static class MessageChannelCallback implements Server.NotifyCallback
/*     */   {
/*     */     public void dataReceived(String header, ByteBuffer buffer)
/*     */     {
/*     */       try {
/* 117 */         Message m = Message.create(buffer);
/* 118 */         m.getHandler().handle(m, header);
/*     */       } catch (IOException e) {
/* 120 */         MessageChannel.log.warn("can't create message", e);
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 129 */   private static Log log = LogFactory.getLog("iochannel");
/*     */   private static MessageChannel instance;
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/iochannel/MessageChannel.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */